//引入VueRouter
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/LoginPage.vue'
import HomePage from '@/components/HomePage.vue'
import OverallDisplay from '../components/OverallDisplay.vue'
import GoodsCategory from "@/components/GoodsCategory.vue";
import GoodsSort from "@/components/GoodsSort.vue";
import UserList from "@/components/UserList.vue";
import RoleList from "@/components/RoleList.vue";
import AddProduct from "@/components/AddProduct.vue";
import ProductPromotion from "@/components/ProductPromotion.vue";
import ProductAttribute from "@/components/ProductAttribute.vue";
import ProductRelation from "@/components/ProductRelation.vue";
import axios from 'axios'
import store from '../store'
//引入路由组件
Vue.use(VueRouter);
const router = new VueRouter({
    routes: [
        {
            path: '/',
            component: Login,
        },
        {
            path: '/home',
            name: 'HomePage',
            component: HomePage,
            redirect: '/home/overallDisplay', // 添加重定向
            children: [
                // {
                //     path: '', // 表示默认子路由，也就是进入 /home 后默认显示的页面
                //     component: OverallDisplay // 修改为对应的子路由路径字符串形式
                // },
                {
                    path: 'overallDisplay',
                    name: 'overallDisplay',
                    component: OverallDisplay
                },
                {
                    path: 'goodsCategory',
                    name: 'GoodsCategory',
                    component: GoodsCategory
                },
                {
                    path: 'goodsSort',
                    name: 'GoodsSort',
                    component: GoodsSort
                },
                {
                    path: 'userList',
                    name: 'UserList',
                    component: UserList
                },
                {
                    path: 'roleList',
                    name: 'RoleList',
                    component: RoleList
                },
                {
                    path:'addProduct',
                    name:'AddProduct',
                    component: AddProduct
                },
                {
                    path: '/home/addProduct/productPromotion',
                    name: 'ProductPromotion',
                    component: ProductPromotion
                },
                {
                    path: '/home/addProduct/productAttribute',
                    name: 'ProductAttribute',
                    component: ProductAttribute
                },
                {
                    path: '/home/addProduct/productRelation',
                    name: 'ProductRelation',
                    component: ProductRelation
                }
            ]


        }
    ],
})


router.beforeEach((to, from, next) => {
    if (to.path === '/') {
        window.localStorage.removeItem('userData')
        next()
    } else {
        let admin = JSON.parse(window.localStorage.getItem('userData')
        )
        if (!admin) {
            next({ path: '/' })
        } else {
            axios({
                url: 'http://localhost:8080/login/check_Token',
                method: 'get',
                headers: {
                    token: admin.token
                }
            }).then((response) => {
                if (!response.data) {
                    console.log('校验失败')
                    store.commit('user/SET_MESSAGE', {
                        show: true,
                        type: 'error',
                        content: '身份过期，请重新登录'
                    })
                    next({ path: '/' })
                } else {
                    next()
                }
            }).catch((error) => {
                console.log('请求出错：', error)
                // 出现请求错误时，重定向到登录页面或者根据业务需求做其他处理
                next({ path: '/' })
            })
        }
    }
})
//暴露router
export default router
